package com.project.controller.web;

import com.project.common.utils.MyPageHelper;
import com.project.entity.TeacherInfo;
import com.project.myBeans.R;
import com.project.myBeans.ResponseBean;
import com.project.myBeans.TeacherInfoDetail;
import com.project.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/27 17:27
 * @param:
 * @return:
 */
@EnableAutoConfiguration //自动载入应用程序所需的所有Bean
@RestController //@Controller与@ResponseBody的合并
@RequestMapping(value = "/teacherInfo")
@Api(description = "教师信息相关",value = "teacherInfo")
public class TeacherInfoController {
    @Autowired
    private TeacherInfoService teacherInfoService;
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private UniversityService universityService;
    @Autowired
    private UserService userService;
    @Autowired
    private TeacherService teacherService;
    /**
     * TateBrown 2018年5月27日
     *
     * @param teacherInfoDetail
     * @return
     */
    @ApiOperation("根据id更新用户表信息 创建教师信息表信息")
    @PostMapping("/addDetailOne")
    public R addDetailOne(@RequestBody TeacherInfoDetail teacherInfoDetail) {
        try {
            teacherInfoService.addDetailOne(teacherInfoDetail);
           return R.ok("添加成功");
        } catch (Exception e) {
            return R.error("程序异常");

        }


    }

    /**
     * TateBrown 2018年5月27日
     *
     * @param teacherInfoDetail
     * @return
     */
    @ApiOperation("根据id更新用户表信息 根据id更新教师信息表")
    @PostMapping("/modifyDetailOne")
    public R modifyDetailOne(@RequestBody TeacherInfoDetail teacherInfoDetail) {

        try {
            teacherInfoService.modifyDetailOne(teacherInfoDetail);
            return R.ok("修改成功");
        } catch (Exception e) {
            return R.error("程序异常");

        }

    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param userId
     * @return
     */
    @ApiOperation("按userId查找教师信息")
    @GetMapping("/findOneDetail/{userId}")
    public R findOneDetail(@PathVariable("userId") Integer userId) {
        try {
            TeacherInfoDetail teacherInfoDetail = teacherInfoService.findOneDetailByUserId(userId);
            return R.ok("查找成功").put("data",teacherInfoDetail);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查找失败");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param universityid
     * @return
     */
    @ApiOperation("根据大学id批量查询教师信息")
    @GetMapping("/findBatchByUniversityid/{universityid}")
    public R findBatchByUniversityid(@PathVariable("universityid") Integer universityid){

        try {
            return R.ok().put("data",teacherInfoService.findBatchByUniversityid(universityid));
        } catch (Exception e) {
            return R.error("程序异常");
        }

    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param teacherInfo
     * @return
     */
    @ApiOperation("根据schoolid查找老师详细信息")
    @PostMapping("/findDetailBatchBySchoolIdCondition")
    public R findDetailBatchBySchoolIdCondition(@RequestBody TeacherInfo teacherInfo){
        try {
            /*if (teacherInfo.getPageIndex() > 0) {
                teacherInfo.setPageIndex(teacherInfo.getPageIndex()-1);
            }*/
            MyPageHelper.setPageParam(teacherInfo);
            List<TeacherInfoDetail> teacherInfoDetailList = teacherInfoService.findBatchBySchoolIdCondition(teacherInfo);
            return R.ok("查找成功").put("data",teacherInfoDetailList).put("total", MyPageHelper.getTotal(teacherInfoDetailList));
        } catch (Exception e) {
            e.printStackTrace();
           return R.error("程序异常");
        }
    }

    /**
     * TateBrown 2018年5月27日
     *
     * @param teacherInfoDetail
     * @return
     */
    @ApiOperation("创建教师账号")
    @PostMapping("/addTeacherAccount")
    public R addTeacherAccount(@RequestBody TeacherInfoDetail teacherInfoDetail){
        try {
            if (teacherService.addDetailOne(teacherInfoDetail.getUser(), teacherInfoDetail.getTeacherInfo())){
                return R.ok("创建成功");
            }
            else {
                return R.error("用户名已存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
}
